home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / dxcmds34.sit / Dartmouth XCMD's 3.4.3 / card_20223.txt < prev    next >
Text File  |  1990-04-17  |  14KB  |  488 lines

  1. -- card: 20223 from stack: in.3
  2. -- bmap block id: 0
  3. -- flags: 4000
  4. -- background id: 16896
  5. -- name: FindKey
  6. ----- HyperTalk script -----
  7. on opencard
  8.   global nextPos
  9.   hide card field "source"
  10.   set the scroll of card field "documentation" to 0
  11.   hide button "arrow"
  12.   put 0 into nextPos
  13.   set the loc of btn arrow to "312,40"
  14.   pass opencard
  15. end opencard
  16.  
  17. on Install
  18.   get ChooseTargetStack()
  19.   InstallResource XFCN,FindKey,it
  20. end Install
  21.  
  22.  
  23. -- part 3 (button)
  24. -- low flags: 00
  25. -- high flags: A004
  26. -- rect: left=337 top=236 right=271 bottom=456
  27. -- title width / last selected line: 0
  28. -- icon id / first selected line: 0 / 0
  29. -- text alignment: 1
  30. -- font id: 0
  31. -- text size: 12
  32. -- style flags: 0
  33. -- line height: 16
  34. -- part name: Find Reds
  35. ----- HyperTalk script -----
  36. on mouseUp
  37.   global nextPos
  38.   get FindKey(card field demo,"red",4,false,nextPos)
  39.   put it into found
  40.   put item 2 of found into atLine
  41.   if atLine is 0 then
  42.     answer "No more found."
  43.     put "312,40" into newLoc
  44.   else
  45.     put the textheight of card field demo into th
  46.     get the rect of card field demo
  47.     put item 2 of it into fTop
  48.     put item 1 of the loc of btn "arrow" into newLoc
  49.     put fTop+(atLine * th)-7 into item 2 of newLoc
  50.   end if
  51.   set the loc of btn arrow to newLoc
  52.   show card btn arrow
  53.   set the cursor to hand
  54.   put item 1 of found + 1 into nextPos
  55. end mouseUp
  56.  
  57.  
  58.  
  59. -- part 6 (button)
  60. -- low flags: 00
  61. -- high flags: 8003
  62. -- rect: left=304 top=299 right=321 bottom=425
  63. -- title width / last selected line: 0
  64. -- icon id / first selected line: 0 / 0
  65. -- text alignment: 1
  66. -- font id: 0
  67. -- text size: 12
  68. -- style flags: 0
  69. -- line height: 16
  70. -- part name: Show C Source
  71. ----- HyperTalk script -----
  72. on mouseUp
  73.   get the visible of card field "source"
  74.   set the visible of card field "source" to not it
  75.   if it is false then
  76.     set the name of me to "Hide C Source"
  77.   else
  78.     set the name of me to "Show C Source"
  79.   end if
  80. end mouseUp
  81.  
  82.  
  83.  
  84. -- part 8 (field)
  85. -- low flags: 01
  86. -- high flags: 0004
  87. -- rect: left=323 top=35 right=184 bottom=486
  88. -- title width / last selected line: 0
  89. -- icon id / first selected line: 0 / 0
  90. -- text alignment: 0
  91. -- font id: 3
  92. -- text size: 9
  93. -- style flags: 0
  94. -- line height: 12
  95. -- part name: demo
  96.  
  97.  
  98. -- part 9 (field)
  99. -- low flags: 01
  100. -- high flags: 2007
  101. -- rect: left=18 top=33 right=287 bottom=295
  102. -- title width / last selected line: 0
  103. -- icon id / first selected line: 0 / 0
  104. -- text alignment: 0
  105. -- font id: 3
  106. -- text size: 10
  107. -- style flags: 0
  108. -- line height: 13
  109. -- part name: Documentation
  110.  
  111.  
  112. -- part 10 (button)
  113. -- low flags: 00
  114. -- high flags: A004
  115. -- rect: left=336 top=192 right=230 bottom=456
  116. -- title width / last selected line: 0
  117. -- icon id / first selected line: 0 / 0
  118. -- text alignment: 1
  119. -- font id: 0
  120. -- text size: 12
  121. -- style flags: 0
  122. -- line height: 16
  123. -- part name: Find Fords
  124. ----- HyperTalk script -----
  125. on mouseUp
  126.   global nextPos
  127.   get FindKey(card field demo,"Ford",1,false,nextPos)
  128.   put it into found
  129.   put item 2 of found into atLine
  130.   if atLine is 0 then
  131.     answer "No more found."
  132.     put "312,40" into newLoc
  133.   else
  134.     put the textheight of card field demo into th
  135.     get the rect of card field demo
  136.     put item 2 of it into fTop
  137.     put item 1 of the loc of btn "arrow" into newLoc
  138.     put fTop+(atLine * th)-7 into item 2 of newLoc
  139.   end if
  140.   set the loc of btn arrow to newLoc
  141.   show card btn arrow
  142.   set the cursor to hand
  143.   put item 1 of found + 1 into nextPos
  144. end mouseUp
  145.  
  146.  
  147.  
  148. -- part 11 (button)
  149. -- low flags: 00
  150. -- high flags: 0000
  151. -- rect: left=303 top=29 right=51 bottom=321
  152. -- title width / last selected line: 0
  153. -- icon id / first selected line: 16560 / 16560
  154. -- text alignment: 1
  155. -- font id: 0
  156. -- text size: 12
  157. -- style flags: 0
  158. -- line height: 16
  159. -- part name: arrow
  160.  
  161.  
  162. -- part 7 (field)
  163. -- low flags: 81
  164. -- high flags: 0007
  165. -- rect: left=18 top=31 right=290 bottom=489
  166. -- title width / last selected line: 0
  167. -- icon id / first selected line: 0 / 0
  168. -- text alignment: 0
  169. -- font id: 3
  170. -- text size: 10
  171. -- style flags: 0
  172. -- line height: 13
  173. -- part name: Source
  174.  
  175.  
  176. -- part contents for card part 7
  177. ----- text -----
  178. /* FindKey1.0.c */
  179. /* ┬⌐ Trustees of Dartmouth College */
  180. /* written in LightSpeed C  ┬⌐ Think Technologies, Inc */
  181. /* by Roger Brown 7/21/88  Courseware Development group */
  182.  
  183. /* From FindInField1.4.c */
  184.  
  185.  
  186. /* This is a HyperCard XFCN that locates a target string in a specified
  187.    item of an input field (or other container) and returns the byte 
  188.    position in the field, the number  of the line where it was find, 
  189.    and the byte position in that line.
  190.    
  191.    Case sensitivity is used if an optional fourth parameter is "true".
  192.    An optional fifth parameter can be sent to specify the byte position
  193.    in the field where the search should start.
  194.     
  195.    Syntax is:
  196.    
  197.         get FindKey(container,target,items,"true",startByte)
  198.    
  199.    ex.      get FindKey(card field 1,"***","3,4")
  200.   
  201.    returns: item 1: byte offset in field or 0 if not found
  202.             item 2: line of card field 1 that contains '***' or 0
  203.             item 3: byte position in the line or 0
  204.             item 4: the item where it was found
  205.        
  206.    where    container   is any hypercard container (field, variable), presumed to be 
  207.                         multi-lined.
  208.             target      is the string to look for 
  209.             items       an item list of numbers that designate item
  210.                         positions to look in (defaults to 1)  
  211.             true        is an optional parameter turning on case sensitivity   
  212.             startByte   is an optional parameter specifying the byte position
  213.                         in the container where the search should start.
  214.                                
  215.       To compile: create a project with this and MacTraps. Build as code resource type
  216.                   XFCN named FindKey.
  217. */
  218.  
  219.  
  220.  
  221. #include "stddata_ctype.c"
  222. #include "strings.c"
  223. #include "ToolboxUtil.h"
  224. #include "HyperXCmd.h"
  225. #include "XCmdGlue.inc.c"
  226. #include "SetUpA4.h"
  227.  
  228. /* change a string to all upper case */
  229.  
  230. int toupper(c)
  231. char    c;
  232. {
  233.     return( (c>='a')&&(c<='z') ? (c-('a'-'A')) : c );
  234. }
  235.  
  236. /* see if two characters match, maybe ignoring case */
  237.  
  238. matchChar(a,b,theCase)
  239. char a,b;
  240. int theCase;
  241. {
  242.     if (theCase==1) return (a==b);
  243.     return (toupper(a)==toupper(b));
  244. }
  245.  
  246. /* build the return result string from numeric parts */
  247.  
  248. BuildResult(fPos,fLine,lPos,item,result)
  249. int fPos,fLine,lPos,item;
  250. char *result;
  251. {
  252.     char temp[64];
  253.     
  254.     NumToString((long)fPos,temp);
  255.     PtoCstr((char*)temp);
  256.     strcpy(result,temp);
  257.     strcat(result,",");
  258.     NumToString((long)fLine,temp);
  259.     PtoCstr((char*)temp);
  260.     strcat(result,temp);
  261.     strcat(result,",");
  262.     NumToString((long)lPos,temp);
  263.     PtoCstr((char*)temp);
  264.     strcat(result,temp);
  265.     strcat(result,",");
  266.     NumToString((long)item,temp);
  267.     PtoCstr((char*)temp);
  268.     strcat(result,temp);
  269. }
  270.  
  271. ItemInList(item,items,numItems)
  272. int item;
  273. int items[32];
  274. int numItems;
  275. {
  276.         int i;
  277.         for (i=0;i<numItems;i++)
  278.                 if (items[i]==item) return 1;
  279.         return 0;
  280. }
  281.  
  282. /* find the target as specified */
  283.  
  284. FindIt(theField,theTarget,items,caseSens,result,start)
  285. char *theField;          /* pointer to field to search */
  286. char *theTarget;         /* pointer to target string */
  287. char *items;             /* the list of items to look in */
  288. char *result;            /* pointer to the return result string */
  289. int caseSens,start;      /* case sensitive = 1 for yes, start is where (byte) to start */
  290. {
  291.     int c;             /* index of current character */
  292.     int tLen;          /* target length */
  293.     int line;          /* line currently being searched */
  294.     int lPos;          /* character position in line */
  295.     int item;          /* item number in line */
  296.     register char *fc,*tc; /* pointers to field and target characters */
  297.     int itemList[32];     /* can look for up to this many */
  298.     int numItems;       /* how many to look for */
  299.     int i;              /* loop counter */
  300.     Str255 numStr;
  301.     long lNum;
  302.     
  303.     
  304.     /* get the list of items to look in */
  305.     
  306.     numItems = NumHCItems(items);
  307.     if (numItems > 32) numItems = 32;
  308.     for (i=0;i<numItems;i++) {
  309.         itemList[i] = GetHCItem(items,i+1,numStr);
  310.      }
  311.     
  312.     
  313.     tLen = strlen(theTarget);         /* assume this is short */
  314.     c = lPos = 0; 
  315.     line = item = 1;                            /* offset into theField */
  316.     fc = theField;                       /* pointer into theField */
  317.     while (*fc != 0) {                   /* scan to end */
  318.         tc = theTarget;                  /* pointer to theTarget */
  319.         if (*fc==13) {
  320.              line++;                     /* passing a line boundary */
  321.              lPos = -1;                  /* reset position */
  322.              item = 1;                   /* reset item */
  323.         }
  324.         if (*fc==44) item++;
  325.         if ((c+1) > start) {             /* don't go until something matches */
  326.             while (matchChar(*tc,*fc,caseSens)) {   /* check for match */
  327.                 fc++;                      /* cycle through matching characters */
  328.                 tc++;
  329.                 if ((tc == (theTarget+tLen))&&  /* all target characters matched */
  330.                     (ItemInList(item,itemList,numItems)==1)) {  /* and its the right item */
  331.                     BuildResult(c+1,line,lPos+1,item,result);
  332.                     return;  /* matched */
  333.                 }
  334.             }
  335.         }
  336.         c++;                          /* move along */
  337.         lPos++;
  338.         fc = theField + c;            /* reset pointer after a search */
  339.     }
  340.     BuildResult(0,0,0,0,result);
  341.     return;                         /* target not found */
  342. }
  343.  
  344. /* XCMD entry */
  345.  
  346. pascal void FindKey(paramPtr)
  347. XCmdBlockPtr    paramPtr;
  348. {   
  349.     int i;
  350.     Str255 paramStr,theResult;
  351.     Ptr theField,theTarget,theItems,casePtr,theStart;
  352.     long line;
  353.     Size len;
  354.     CursHandle theCursor;
  355.     Handle resultHandle;
  356.     int theCase;
  357.     long startAt;
  358.     
  359.     /* put up a watch */
  360.     
  361.     theCursor = GetCursor(watchCursor);
  362.     SetCursor(*theCursor);
  363.     
  364.     /* lock parameters down so we can point to them */
  365.     
  366.     for (i=0;i<paramPtr->paramCount;i++) {
  367.         MoveHHi(paramPtr->params[i]);
  368.         HLock(paramPtr->params[i]);
  369.     }
  370.     
  371.     /* get the parameters */
  372.     
  373.     theField = *(paramPtr->params[0]);   /* the container */
  374.     theTarget = *(paramPtr->params[1]);  /* the target string */
  375.     theItems = *(paramPtr->params[2]);  /* the target item number */
  376.     
  377.     if (paramPtr->paramCount>3)  {       /* case sensitivity flag */
  378.         casePtr = *(paramPtr->params[3]);
  379.         strcpy(paramStr,casePtr);
  380.     }
  381.     else strcpy(paramStr,"");
  382.     for (i=0;i<strlen(paramStr);i++) paramStr[i] = toupper(paramStr[i]);
  383.     if (strcmp(paramStr,"TRUE")==0) theCase = 1;
  384.     else theCase = 0;
  385.     
  386.     if (paramPtr->paramCount>4) {        /* start byte */
  387.         theStart = *(paramPtr->params[4]);
  388.         strcpy(paramStr,theStart);
  389.         CtoPstr((char *)paramStr);
  390.         StringToNum(paramStr,&startAt);
  391.     }
  392.     else startAt = 0;
  393.        
  394.     /* do it */
  395.     
  396.     FindIt(theField,theTarget,theItems,theCase,theResult,(int)startAt);
  397.     
  398.     /* assemble the return string */
  399.     
  400.     len = 1+strlen(theResult);
  401.     resultHandle = NewHandle(len);
  402.     HLock(resultHandle);
  403.     BlockMove(theResult,*resultHandle,len);
  404.     HUnlock(resultHandle);
  405.     paramPtr->returnValue = resultHandle;
  406.     for (i=0;i<paramPtr->paramCount;i++) HUnlock (paramPtr->params[i]);
  407.     return;
  408. }
  409.  
  410. pascal void main(paramPtr)
  411. XCmdBlockPtr    paramPtr;
  412. /* this is the entry point for the XFCN */
  413. {
  414.     RememberA0();
  415.     SetUpA4();                  /* to access globals */
  416.     FindKey(paramPtr);      /* run the main event loop */  
  417.     RestoreA4(); 
  418.     return;
  419. }
  420.  
  421.  
  422.  
  423.  
  424. -- part contents for card part 9
  425. ----- text -----
  426. FindKey version 1.0
  427. Roger Brown
  428.  
  429. This is a HyperCard XFCN that locates a target string in a specified item of an input field (or other container) and returns the byte position in the field, the number of the line where it was found,  and the byte position in that line.
  430.    
  431.    Case sensitivity is used if an optional fourth parameter is "true".  An optional fifth parameter can be sent to specify the byte position  in the field where the search should start.
  432.     
  433. INVOKING FindKey
  434.  
  435.         get FindKey (container,target,items,"true",                
  436.                              startByte)
  437.         where    container  is any HyperCard             
  438.                                      container (field, variable), 
  439.                                      presumed to be  
  440.                                      multi-lined.
  441.                       target     is the string to look for 
  442.                       items      an item list of numbers 
  443.                                      that designate item 
  444.                                      positions to look in (
  445.                                      defaults to 1)  
  446.                       true        is an optional parameter 
  447.                                      turning on case sensitivity   
  448.                       startByte  is an optional parameter 
  449.                                      specifying the byte 
  450.                                      position  in the container               
  451.                                      where the search should          
  452.                                      start.
  453.  
  454.   
  455.         returns: item 1: byte offset in field or 0 if 
  456.                                    not found
  457.                      item 2: line of card field 1 that          
  458.                                  contains the target string 
  459.                                  or 0
  460.                      item 3: byte position in the line or 0
  461.                      item 4: the item where it was found
  462.        
  463.  
  464. EXAMPLE
  465.  
  466.   ex.      get FindKey(card field 1,"***","3,4")
  467.        
  468. This example will look in card field 1 for the first occurrence of the string *** in item 3 or item 4 of any line. 
  469.  
  470.  
  471. REVISION HISTORY
  472. 1.0 First public release. 
  473.  
  474. -- part contents for card part 8
  475. ----- text -----
  476. Chevrolet, BelAir,1957,blue
  477. Ford,Thunderbird,1960,red
  478. Packard,Roadster,1939,black
  479. Kaiser,Jeep,1969,yellow
  480. Chevrolet, Impala,1967,red
  481. Ford,Fairlane,1970,white
  482. Plymouth,Reliant,1989,black
  483. Mercury,Cougar,1975,white
  484. VolksWagen, Beetle,1967,blue
  485. Ford,Mustang,1960,red
  486. Pontiac,GTO,1964,black
  487. Lincoln,Continental,1969,red
  488.